package com.enn.dxg.bigscreen;

import android.Manifest;
import android.annotation.SuppressLint;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.blankj.utilcode.util.PermissionUtils;
import com.enn.dxg.bigscreen.utils.JSObject;
import com.enn.dxg.bigscreen.utils.MyWebChromeClient;
import com.enn.dxg.bigscreen.utils.MyWebViewClient;

/**
 * 原始的webView
 */
public class FullscreenActivity extends AppCompatActivity {

    private WebView mWebView;
    ProgressBar myProgressBar;
    private MyWebViewClient WVClient;
    private WebSettings webSettings;
    private MyWebChromeClient chromeClient;
    //封装接收js调用Android的方法类
    private JSObject jsobject;
    private String web_url = Constant.web_url;
    //异步请求
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1000:
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        init();
        initView();
    }

    private void init() {
        mWebView = (WebView) findViewById(R.id.web_view);
        myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
        WVClient = new MyWebViewClient();
        chromeClient = new MyWebChromeClient();
        jsobject = new JSObject(FullscreenActivity.this, mHandler);
    }

    private void initView() {
        webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSavePassword(false);
        //支持多种分辨率，需要js网页支持
        //webSettings.setUserAgentString("mac os");
        //webSettings.setDefaultTextEncodingName("utf-8");
        //zyj新加
        mWebView.clearCache(true);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        webSettings.setSupportZoom(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDisplayZoomControls(false);
        //显示本地js网页
        //mWebView.loadUrl("file:///android_asset/test.html");
//        mWebView.loadUrl("https://www.baidu.com");
        mWebView.loadUrl(web_url);
        Log.d("hpugs", web_url);
        mWebView.setWebViewClient(WVClient);
        mWebView.setWebChromeClient(new WebChromeClient() {// 为webview添加进度条
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.INVISIBLE);
                } else {
                    if (View.INVISIBLE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
//					System.out.println("newProgress      " + newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        mWebView.setWebViewClient(
                new WebViewClient() {
                    @Override
                    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                        handler.proceed();
                    }
                });
        //注意第二个参数android，这个是JS网页调用Android方法的一个类似ID的东西
        mWebView.addJavascriptInterface(jsobject, "android");
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
//            mHandler.post(new Runnable() {
//                @Override
//                public void run() {
//                    mWebView.loadUrl("javascript:androidKeyBack('调用js代码')");
//                }
//            });
            if (mWebView.canGoBack()) {
                mWebView.goBack();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
